package com.lzs.bootsafety.business.sys.util;

import com.lzs.bootsafety.business.sys.entity.SysDict;
import com.lzs.bootsafety.business.sys.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
@Component
public class DictUtil {
    public static   HashMap<String, String> hashMap = new HashMap<>();
    @Autowired
    private  SysDictMapper sysDictMapper;
    public static DictUtil dictUtil;
    @PostConstruct
    public void init(){
        dictUtil = this;
        List<SysDict> sysDictList = sysDictMapper.selectSysDict(null);
        for (int i = 0; i < sysDictList.size(); i++) {
            SysDict sysDict = sysDictList.get(i);
            String dictLabel = sysDict.getDictLabel();
            String dictType = sysDict.getDictType();
            String key = dictType+"_"+dictLabel;
            hashMap.put(key,sysDict.getDictValue());
        }
    }
    public static HashMap<String, String> getDictList(){
        return hashMap;
    }
    public static String getDictValue(String dictLabel,String dictType){
        StringBuilder sb = new StringBuilder();
        StringBuilder sbKey = sb.append(dictType).append("_").append(dictLabel);
        String key = sbKey.toString();
        String value = hashMap.get(key);
        if ("".equalsIgnoreCase(value)) {
            dictUtil.init();
            getDictValue(dictLabel, dictType);
        }
        return value;
    }
}
